Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

19.4 Event Tracing Subsystem Initialization

The initialization function for the event tracing subsystem, trace_event_init(), is located within the source file git/kernel/trace/trace_events.c, and its definition is implemented as follows:

void __init trace_event_init(void)
{
	event_trace_memsetup();
	init_ftrace_syscalls();
	event_trace_enable();
	event_trace_init_fields();
}

This code acts as the central execution entry point for initializing the event tracing infrastructure during the kernel boot stage, establishing the foundational architecture for kernel observability. Below is a detailed breakdown of the critical steps executed within this function.

  • event_trace_memsetup() allocates memory within the kernel lookaside caches for two highly critical tracking structures: struct ftrace_event_field and struct trace_event_file.

    • The ftrace_event_field structure describes each individual data field contained within a tracking event (capturing metrics such as the field name, memory offset, and data size).

    • The primary architectural role of the trace_event_file structure is to bind the static definition of a compile-time tracepoint to a specific runtime tracing instance. Guided by this mapping, the kernel is empowered to dynamically construct individual control entries for each event under the virtual directory tree path /sys/kernel/tracing/events/. This routine initializes the core lists and hash tables used to store struct trace_event_call entries (the master data structures defining each event type), ensuring that subsequent event registration cycles have a dedicated repository available to hold their tracking descriptors.

  • init_ftrace_syscalls() serves as a core mechanism within the function tracer framework, responsible for bootstrapping the metadata records used to track system calls. It evaluates system calls systematically by driving a for loop that iterates through every valid syscall number from 0 up to NR_syscalls. The function first executes arch_syscall_addr(i) to resolve the internal kernel function entry address corresponding to the i-th system call. Leveraging this address, it resolves the location of the matching system call metadata descriptor, enabling ftrace to target and trace that specific system call. It then updates the syscall_nr field within the resolved metadata structure to match the active loop index i. If the compilation options omit CONFIG_HAVE_SPARSE_SYSCALL_NR (meaning the system call numbers scale sequentially without gaps), the metadata is saved directly into the continuous array slot syscalls_metadata[i]. Conversely, if sparse numbering is active in the build parameters, the engine leverages the advanced XArray subsystem via xa_store() to preserve the metadata entry within an optimized search tree named syscalls_metadata_sparse.

  • event_trace_enable() energizes the event subsystem and hooks its control interfaces onto the active debugfs/tracefs virtual file system layers. It registers all compiled trace_event items onto the global tracking chains. Upon completing this registration stage, the kernel begins instantiating individual subdirectory nodes for each distinct architectural subsystem (such as sched, irq, and kmem) under the parent path /sys/kernel/tracing/events/, creating the corresponding control and layout files including enable and format. It concurrently parses the command-line arguments specified during kernel boot (such as evaluating options formatted as trace_event=...), enabling the subsystem to turn on tracing for specific events immediately during early boot.

The inner execution workflow of this routine is implemented as follows: while init_ftrace_syscalls() focuses on preparing data structures, this function formally sets up the execution landscape. Utilizing the for_each_event macro helper, the routine loops through all available tracking events embedded by the linker toolchain within the bounded memory segment situated between __start_ftrace_events and __stop_ftrace_events. It first dispatches event_init() to execute baseline initialization checks on each event, and appends the successfully initialized event descriptors directly into the master tracking list ftrace_events.

Upon processing all compiled events, the function triggers __trace_early_add_events() to generate the logic representations of the events in memory before the physical virtual filesystem trees are mounted. The operating system mandates that the root tracking instance (top_trace_array) must possess operational tracepoints before debugging filesystems (such as debugfs or tracefs) are formally mounted at the path /sys/kernel/debug/tracing.

Once these valid tracking nodes are active in memory, the function calls early_enable_events(). This routine audits the parameters specified during system boot (such as scanning for the string directive trace_event=...) and activates the requested tracing targets during this early bootstrap phase.

At its conclusion, the routine registers several central interactive components onto the kernel infrastructure:

  • trace_printk_start_comm(): Authorizes the trace_printk engine to capture the literal string names of executing processes, a tracking capability that is critical when analyzing generated trace diagnostic logs.

  • register_event_cmds(): Registers the standard runtime command modules associated with event tracking management.

  • register_trigger_cmds(): Registers event-driven triggers, enabling the tracing infrastructure to execute specific actions (such as generating a trace snapshot or executing a kernel stack backtrace) at the exact moment a target event fires.

  • event_trace_init_fields() initializes the individual tracking elements enclosed within the event field descriptor structure struct ftrace_event_field. Every individual tracing event maps to a distinct collection of payload fields (such as recording process IDs, instruction pointers, or customized data structures). This function iterates across all successfully registered system events to parse and initialize their data field formatting criteria. This layout dictates the exact text definition and output schema exposed to user space whenever a developer executes the command cat /sys/kernel/tracing/events/xxx/format.

Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com